import type { ExerciseI, ExercisePlanI, GetExerciseListParamsI } from '@/types/exercise'
import {
  queryExerciseFilters,
  queryExerciseByIds,
  queryExerciseCollect,
  queryExerciseFiltersTotal,
  executeUpsertExerciseCollect,
  executeDeleteExerciseCollect,
  queryExerciseFiltersNotLimit,
  queryExerciseListByName,
} from '@/AGC/manager/exercise'
import { useUserStore } from '@/stores/user'
import { ExerciseCollect } from '@/AGC/model/ExerciseCollect'
import type { UserFiltersI } from '@/types/user'
import { getExerciseDislikeIdsUtil } from './dislike'

/**
 * 根据运动名称获取运动列表
 * @param name 运动名称
 * @returns 运动列表
 */
export const getExerciseListByNameUtil = async (name: string) => {
  const list = await queryExerciseListByName(name)

  return list.map((item) => {
    return {
      name: item.getName(),
      exercise_amount: item.getExercise_amount(),
      unit: item.getUnit(),
      calories: item.getCalories(),
    } as ExerciseI
  })
}

/**
 * 根据运动计划获取运动列表
 * @param plan 运动计划
 * @returns 运动列表
 */
export const getExerciseListByPlanUtil = async (plan: ExercisePlanI) => {
  // 1. 根据性别和运动能力获取运动筛选条件
  const filters = await queryExerciseFiltersNotLimit(useUserStore().userFilter as UserFiltersI)

  // 2. 获取运动 ids
  const exerciseIds = filters.map((item) => item.getExercise_id()!)

  // 3. 获取用户不喜欢的运动 ids
  const dislikeIds = await getExerciseDislikeIdsUtil()

  // 4. 获取用户喜欢的运动 ids
  const likeIds = exerciseIds.filter((item) => !dislikeIds.includes(item))

  // 5. 获取运动列表
  let exerciseList = await queryExerciseByIds(likeIds)

  // 6. 根据热量筛选
  exerciseList = exerciseList.filter((item) => {
    const calories = (plan.amount / item.getExercise_amount()) * item.getCalories()
    return calories >= plan.calories - 20 && calories <= plan.calories + 20
  })

  // 7. 排除自身
  exerciseList = exerciseList.filter((item) => item.getName() != plan.exercise_name)

  // 8. 返回运动列表
  return exerciseList.map((item) => {
    return {
      id: item.getId()!,
      name: item.getName()!,
      exercise_amount: item.getExercise_amount()!,
      unit: item.getUnit()!,
      calories: item.getCalories()!,
      status: 0,
    } as ExerciseI
  })
}

/**
 * 获取整合后的运动数据
 * @param filter 筛选条件--GetExerciseListParamsI
 * @returns 整合后的运动数据
 */
export const getExerciseByFilterUtil = async (filter: GetExerciseListParamsI) => {
  // 获取运动筛选条件
  const exerciseFilters = await queryExerciseFilters(filter)
  // 提取运动筛选条件 ids
  const ids = exerciseFilters.map((exerciseFilter) => exerciseFilter.getExercise_id()!)
  // 获取运动
  const exercises = await queryExerciseByIds(ids)
  // 获取用户收藏的运动
  const user_id = useUserStore().userInfo.id
  const exerciseCollects = await queryExerciseCollect(user_id)
  // 提取用户收藏的运动 ids
  const exerciseCollectIds = exerciseCollects.map(
    (exerciseCollect) => exerciseCollect.getExercise_id()!,
  )
  // 整合运动数据
  const data = exercises.map((exercise) => {
    return {
      id: exercise.getId()!,
      name: exercise.getName()!,
      exercise_amount: exercise.getExercise_amount()!,
      unit: exercise.getUnit()!,
      calories: exercise.getCalories()!,
      status: exerciseCollectIds.includes(exercise.getId()!) ? 1 : 0,
    } as ExerciseI
  })
  // 查询满足条件的运动总数
  const total = await queryExerciseFiltersTotal(filter)
  // 返回结果
  return { data, total }
}

/**
 * 创建用户收藏运动
 * @param exercise_id 运动 id
 */
export const createExerciseCollectUtil = async (exercise_id: number) => {
  const user_id = useUserStore().userInfo.id

  const exerciseCollect = new ExerciseCollect()
  exerciseCollect.setUser_id(user_id)
  exerciseCollect.setExercise_id(exercise_id)

  await executeUpsertExerciseCollect(exerciseCollect)
}

/**
 * 删除用户收藏运动
 * @param exercise_id 用户收藏运动id
 */
export const deleteExerciseCollectUtil = async (exercise_id: number) => {
  const user_id = useUserStore().userInfo.id

  const result = await queryExerciseCollect(user_id, exercise_id)

  await executeDeleteExerciseCollect(result)
}
